var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading: !0,
    currentTab: 0,
    userbuystatus: 0,
  },
  openvip() {
    var that = this;

    var openid = wx.getStorageSync('openid');
    if (openid == '') {
      wx.showModal({
        title: '提示',
        content: "请先登录",
        success: function (con) {
          if (con.confirm) {
            wx.navigateTo({
              url: '/hyb_yl/sgin/sgin',
            })
          } else {
            wx.switchTab({
              url: '/hyb_yl/tabBar/index/index',
            })
          }
        }
      })
    } else {
      var money = that.data.info.money;
      var id = that.data.id;
      var title = that.data.info.serh_name;

      app.util.request({
        'url': "entry/wxapp/index.add_service",
        data: {
          title: title,
          s_id: id,
          money: money,
          thumb: that.data.info.serh_thumb,
          openid: openid,
          small_title:that.data.info.serh_ftitle
        },
        success: function (res) {
          var back_orser = res.data.back_orser;
          if (money == '' || money == '0.00' || money == '0') {
            wx.showToast({
              title: '购买成功',
            })
            setTimeout(() => {
              wx.navigateTo({
                url: 'hyb_yl/krecovery/pages/order_lists/order_lists',
              })
            }, 500);
            
          }else{
            app.util.request({
              'url': 'entry/wxapp/huiyuan.pay',
              header: {
                "Content-Type": "application/xml"
              },
              method: "GET",
              data: {
                openid: wx.getStorageSync('openid'),
                payprice: money,
                ordersn:back_orser
              },
              success: function (res) {
                wx.requestPayment({
                  'timeStamp': res.data.timeStamp,
                  'nonceStr': res.data.nonceStr, //字符串随机数
                  'package': res.data.package,
                  'signType': res.data.signType,
                  'paySign': res.data.paySign,
                  success: function (res) {},
                  complete: function (res) {
                    console.log(res)
                    if (res.errMsg != 'requestPayment:fail cancel') {
                      app.util.request({
                        'url': 'entry/wxapp/index.update_service',
                        data: {
                          openid: wx.getStorageSync('openid'),
                          back_orser: back_orser,
                          
                        },
                        success: function (payres) {
                          wx.showToast({
                            title: '支付成功!',
                          })
                          
                          setTimeout(function () {
                            wx.navigateTo({
                              url: 'hyb_yl/krecovery/pages/order_lists/order_lists',
                            })
                          }, 500)
                        },
                      })
                    }
                  }
                })
              }
            })
          }
        }
      })

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      bgc: bgc
    })
    var id = options.id;
    that.setData({
      id: id,
      title: options.title
    })

    that.getService();
  },
  getService: function (e) {
    var that = this;
    app.util.request({
      'url': "entry/wxapp/index.service",
      data: {
        id: that.data.id
      },
      success: function (res) {
        that.setData({
          info: res.data
        })
        wx.setNavigationBarTitle({
          title: res.data.serh_name,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})